package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.bean.ShoppingCart;
import com.itheima.dao.ShoppingCartDao;
import com.itheima.service.ShoppingCartService;
import com.itheima.utils.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartDao shoppingCartDao;

    @Override
    public ShoppingCart add(ShoppingCart shoppingCart) {
        //添加菜品之前要查看此用户之前是否有添加过同类菜品|套餐
        LambdaQueryWrapper<ShoppingCart> lqw =new LambdaQueryWrapper<>();

        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        //lqw.eq(ShoppingCart::getUserId,123L);//测试用



        //判断数据是套餐还是菜品
        if(shoppingCart.getDishId()!=null){
            lqw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        ShoppingCart cartOne = shoppingCartDao.selectOne(lqw);

        //判断菜品是否已经添加过
        if (cartOne!=null){
            cartOne.setNumber(cartOne.getNumber()+1);
            shoppingCartDao.updateById(cartOne);
            return cartOne;
        }
        //没有添加过的话
        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());

        shoppingCart.setUserId(BaseContext.getCurrentId());

        //shoppingCart.setUserId(123L);//测试用

        shoppingCartDao.insert(shoppingCart);
        return shoppingCart;
    }


    @Override
    public List<ShoppingCart> list() {

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        //lqw.eq(ShoppingCart::getUserId, 123L);//测试用

        return shoppingCartDao.selectList(lqw);
    }

    @Override
    public int update(ShoppingCart shoppingCart ) {

        LambdaQueryWrapper<ShoppingCart> lqw =new LambdaQueryWrapper<>();

        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        //lqw.eq(ShoppingCart::getUserId,123L);//测试用

        //判断数据是套餐还是菜品
        if(shoppingCart.getDishId()!=null){
            lqw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        ShoppingCart cartOne = shoppingCartDao.selectOne(lqw);

        //判断菜品|套餐是否只有一份
        if(cartOne.getNumber()==1){
            return shoppingCartDao.deleteById(cartOne);
        }
        //如果大于1的话则数量-1
        cartOne.setNumber(cartOne.getNumber()-1);
        return shoppingCartDao.updateById(cartOne);
    }

    @Override
    public int delete() {
        LambdaQueryWrapper<ShoppingCart> lqw =new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        //lqw.eq(ShoppingCart::getUserId,123L);//测试用
        return shoppingCartDao.delete(lqw);
    }
}
